<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="讲师名称">
        <el-input v-model="lecture.name"/>
      </el-form-item>

      <el-form-item label="讲师排序">
        <el-input-number v-model="lecture.sort" controls-position="right" :min="0"/>
      </el-form-item>

      <el-form-item label="讲师头衔">
        <el-select v-model="lecture.level" clearable placeholder="请选择">
          <el-option :value="1" label="初级讲师"/>
          <el-option :value="2" label="中级讲师"/>
          <el-option :value="3" label="高级讲师"/>
          <el-option :value="4" label="特级讲师"/>
        </el-select>
      </el-form-item>

      <el-form-item label="讲师资历">
        <el-input v-model="lecture.career"/>
      </el-form-item>

      <el-form-item label="讲师简介">
        <el-input v-model="lecture.intro" :rows="10" type="textarea"/>
      </el-form-item>

      <el-form-item label="讲师头像">
        <!--头像缩略图-->
        <pan-thumb :image="lecture.avatar"/>
        <!--文件删除按钮-->
        <el-button type="primary" icon="el-icon-upload" @click="imagecropperShow=true">
          更换头像
        </el-button>

        <image-cropper
          v-show="imagecropperShow"
          :width="300"
          :height="300"
          :key="imagecropperKey"
          :url="BASE_API+'/file/oss'"
          field="file"
          @close="close"
          @crop-upload-success="cropSuccess"/>

      </el-form-item>

      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import lectureApi from "@/api/lecture";
import PanThumb from "@/components/PanThumb";
import ImageCropper from "@/components/ImageCropper"

export default {
  components: {PanThumb,ImageCropper},
  data() {
    return {
      lecture: {
        name: '',
        sort: 0,
        level: 1,
        career: '',
        intro: '',
        avatar: '',
      },
      //上传弹框的组件是否显示
      imagecropperShow:false,
      imagecropperKey: 0,
      BASE_API: process.env.BASE_API,
      saveBtnDisabled: false //保存按钮是否禁用
    }
  },
  watch: {
    //监听
    $route(to, from) { //路由变化执行
      this.init()
    }
  },
  created() {
    this.init()
  },
  methods: {
    close(){
      //关闭上传弹框
      this.imagecropperShow=false
    },
    cropSuccess(data){
      //上传成功
      this.lecture.avatar = data.url
      this.imagecropperShow=false
    }
    ,
    init() {
      if (this.$route.params && this.$route.params.id) {
        const id = this.$route.params.id
        this.lecture = this.getInfo(id)
      } else {
        this.lecture = {}
      }
    },
    saveOrUpdate() {

      if (!this.lecture.id) {
        this.saveLecture()
      } else {
        this.updateLectureInfo()
      }

    },
    //修改讲师的方法
    updateLectureInfo() {
      lectureApi.updateLecture(this.lecture)
        .then(response => {
          this.$message({
            type: 'success',
            message: '修改成功!'
          })
          //路由跳转
          this.$router.push({path: '/lecture/table'})
        })
    }
    ,

    //添加讲师方法
    saveLecture() {
      lectureApi.addLecture(this.lecture)
        .then(response => {
          this.$message({
            type: 'success',
            message: '添加成功!'
          })
          //路由跳转
          this.$router.push({path: '/lecture/table'})
        })
    },
    //根据讲师id查询信息
    getInfo(id) {
      lectureApi.getLectureInfo(id)
        .then(response => {
          this.lecture = response.data.lecture
        })
    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
